//
//  ZJBackPlayeVC.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/6/16.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJBackPlayeVC.h"

@interface ZJBackPlayeVC ()<AVPlayerViewControllerDelegate>
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) AVPlayerViewController  *playerView;
@end

@implementation ZJBackPlayeVC

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keepMoving) name:@"applicationDidBecomeActive" object:nil];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"applicationDidBecomeActive" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //视频播放的url
    NSURL *playerURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"视频"ofType:@"mp4"]];
    
    if (_player) {
        [_player pause];
        _player = nil;
    }
    
    if (_playerView) {
        [_playerView removeFromParentViewController];
        [_playerView.view removeFromSuperview];
        _playerView = nil;
    }
    
    // 1、获取媒体资源地址
    NSString *path = [[NSBundle mainBundle] pathForAuxiliaryExecutable:@"视频.mp4"];
    NSURL *sourceMovieURL = [NSURL fileURLWithPath:path];
    
    // 2、创建AVPlayerItem
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:sourceMovieURL];
    // 3、根据AVPlayerItem创建媒体播放器
    _player = [AVPlayer playerWithPlayerItem:playerItem];
    // 4、创建AVPlayerLayer，用于呈现视频
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
    // 5、设置显示大小和位置
    playerLayer.bounds = self.view.bounds;
    playerLayer.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(playerLayer.bounds));
    // 6、设置拉伸模式
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
    
 //   [self.view.layer addSublayer:playerLayer];
     _playerView = [[AVPlayerViewController alloc]  init];
    // 设置媒体源数据
    _playerView.player = [AVPlayer playerWithURL: [NSURL fileURLWithPath:path]];
    // 设置拉伸模式
    _playerView.videoGravity = AVLayerVideoGravityResize;
    
    // 设置是否显示媒体播放组件
    _playerView.showsPlaybackControls = NO;
    _playerView.view.bounds = self.view.bounds;
    _playerView.view.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(_playerView.view.bounds));
    _playerView.delegate = self;
    [_playerView.player play];
     [self addChildViewController:_playerView];
    [self.view addSubview:_playerView.view];
    [self.view sendSubviewToBack:_playerView.view];
    // 7、获取播放持续时间
//    NSLog(@"%lld", playerItem.duration.value);
//    [_player play];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(runLoopTheMovie:) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    

}

- (void)runLoopTheMovie:(NSNotification *)n{
    //注册的通知  可以自动把 AVPlayerItem 对象传过来，只要接收一下就OK
    
    AVPlayerItem * p = [n object];
    //关键代码
    [p seekToTime:kCMTimeZero];
    
//    [_player play];
    [_playerView.player play];
   // NSLog(@"重播");
}

-(void)keepMoving{
    [_playerView.player play];
}
@end
